package com.kakarote.admin.service.impl;

import cn.hutool.core.date.DatePattern;
import com.kakarote.admin.common.AdminConst;
import com.kakarote.admin.common.AdminModuleEnum;
import com.kakarote.admin.common.ApplyStatusEnum;
import com.kakarote.admin.entity.PO.AdminApply;
import com.kakarote.admin.mapper.AdminApplyMapper;
import com.kakarote.admin.service.IAdminApplyService;
import com.kakarote.core.servlet.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 应用申请信息表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2022-01-07
 */
@Service
public class AdminApplyServiceImpl extends BaseServiceImpl<AdminApplyMapper, AdminApply> implements IAdminApplyService {

    @Override
    public void addApply(AdminModuleEnum moduleEnum, Long companyId) {
        String value = moduleEnum.getValue();
        AdminApply apply = new AdminApply();
        apply.setName(value);
        apply.setCreateTime(LocalDateTime.now());
        apply.setCreateUserId(0L);
        apply.setUpdateUserId(0L);
        apply.setUpdateTime(LocalDateTime.now());
        apply.setCallNumber(null);
        apply.setStatus(ApplyStatusEnum.TIME_OUT.getStatus());
        apply.setEndTime(LocalDateTime.now());
        if (moduleEnum == AdminModuleEnum.ENTERPRISE) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
            LocalDateTime endTime = LocalDateTime.parse("2099-12-31 00:00:00", df);
            apply.setCallNumber(AdminConst.CALL_INIT_NUMBER);
            apply.setEndTime(endTime);
            apply.setStatus(ApplyStatusEnum.NORMAL.getStatus());
        }
        this.addOrUpdate(apply);
    }

    /**
     * 查询字段配置
     *
     * @param id 主键ID
     * @return data
     * @author zhangzhiwei
     * @since 2022-01-07
     */
    @Override
    public AdminApply queryById(Serializable id) {
        return getById(id);
    }

    /**
     * 保存或新增信息
     *
     * @param entity entity
     * @author zhangzhiwei
     * @since 2022-01-07
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdate(AdminApply entity) {
        saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateAdminApplyNumberInfo(String name, Integer number) {
        return getBaseMapper().updateAdminApplyNumberInfo(name, number);
    }

    @Override
    public Integer saveAdminApply(AdminApply adminApply) {
        return getBaseMapper().saveAdminApply(adminApply);
    }

    @Override
    public Integer updateAdminApplyById(AdminApply adminApply) {
        return getBaseMapper().updateAdminApplyById(adminApply);
    }


}
